package cli.menu;

/**
 * BudgetBuoy
 * 
 * Simplify your personal expense management
 * 
 * @team cs2103jan13-w15-3j
 */

//--------------------------------------------------------------

/**
 * AddCashLoan Class
 * 
 * Description: 
 * 
 * 
 * @package   cli.menu
 * @author    Lai Yong Quan (A0080909Y)
 * @edited by: Tan Si Ling (A0085314J)
 */

import shared.Loan;
import cli.tools.Process;

class AddCashLoan extends Function{

	private static final String[] ADDCL = {
		"-= Main menu > Add Entry > Cash Loan =-",
		"[b]: back [u]: undo [r]: redo [h]: home menu",
		"Enter the information in the following format: ",
		"Name / Amount / Lender / Date (DD.MM.YYYY, optional) / Description (optional)",
		"", "Input: " };

	private static State BACK = State.ADDENTRYMENU;
	private static State CURRENT = State.ADDCASHLOAN;

	public static State runMenu() {

		String input = null;
		while(input == null || input.length() == 0){
		cli.print(ADDCL);
		input = cli.nextLine();
		cli.clearScreen();
		}

		if(input.length() == 1){
			char charInput = input.charAt(0);
			if(tools.isHotKey(charInput))
				return hotKeyFunction(charInput);
		}

		return normFunction(input);
	}


	private static State normFunction(String input){

		String[] processing = input.split("/");
		String name,value,lender,details;
		details = "";
		State nextState = CURRENT;
		int[] dmy = tools.initDate();
		// checks if input is valid : at least 2 "attributes" - name and
		// price
		if (processing.length < 3){
			cli.print(INSUFFINFO);
			return CURRENT;
		}

		name = processing[0];
		value = processing[1];
		lender = processing[2];

		// need to see if 3rd entry is date/details
		String unknown;
		// checks price for validity
		int amount = tools.Money2int(value);
		if (amount <= 0){
			cli.print(BADAMT);
			return CURRENT;
		}
		if (processing.length >= 4) {
			unknown = processing[3];

			// checks if it is a date
			try {
				dmy = tools.processDate(unknown);
			} catch (Exception e) {
				// means it is not a valid date
				cli.print(INVALIDDATE);
				// treat 4th entry as details
				details = unknown + " ";
			}

			// checks if there is a details section
			// since user may use '/' characters, we will treat anything
			// from
			// index 3 onwards as details
			if (processing.length >= 5) {
				for (int i = 4; i < processing.length; i++)
					details += processing[i];
			}

		}

		try{
			Loan getLoan = cli.getApi().addCashLoan(name, amount,dmy[0], dmy[1], dmy[2],
					details, amount, lender);
			// Si Ling: **Keeping Track of the Actions DONE**
			tools.action.addToUndoStack((Object) null, getLoan, Process.ADDCASHLOAN,
					(Object) null);
			// **END**
		}
		catch(Exception e){
			cli.print(ERROR);
		}

		return nextState;


	}

	private static State hotKeyFunction(char charInput) {
		State nextState = CURRENT;

		switch (charInput) {
		case 'b':
		case 'B':
			nextState = BACK;
			break;
		case 'h':
		case 'H':
			nextState = State.MAINMENU;
			break;
		case 'u':
		case 'U':
			tools.undo();
			break;
		case 'r':
		case 'R':
			tools.redo();
			break;
		default:
			break;
		}

		return nextState;

	}

}